package com.example.petstore_2.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.petstore_2.common.CommonResponse;
import com.example.petstore_2.domain.User;
import com.example.petstore_2.domain.UserInfo;
import com.example.petstore_2.mapper.UserInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service("userInfoService")
public class UserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    public CommonResponse getUserInfoByUsername(String username) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        if(userInfo == null){
            log.warn(username + "用户不存在");
            return CommonResponse.creatForError("用户不存在");
        }else{
            return CommonResponse.creatForSuccess(userInfo);
        }
    }

    public CommonResponse updateUserInfo(String username, UserInfo userInfo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        userInfoMapper.delete(queryWrapper);
        userInfo.setUsername(username);
        int result = userInfoMapper.insert(userInfo);

        if(result == 1){
            log.info(username + " 用户信息修改成功");
            return CommonResponse.creatForSuccess(userInfoMapper.selectOne(queryWrapper));
        }else{
            log.warn(username + " 用户信息修改失败");
            return CommonResponse.creatForError("修改失败");
        }
    }
}
